﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_frmProductCategoryAddModify : System.Web.UI.Page
{
    clsUser clsUser;
    clsProductCategoryRecord clsProductCategoryRecord;

    #region EVENT METHODS

    protected void Page_Load(object sender, EventArgs e)
    {
        //### Check if session clsUser exists
        if (Session["clsUser"] == null)
        {
            {
                //### Redirect back to login
                Response.Redirect("../frmAdminLogin.aspx");
            }
        }

        clsUser = (clsUser)Session["clsUser"];

        if (!IsPostBack)
        {
            //### If the iProductID is passed through then we want to instantiate the obect with that iProductID
            if (Request.QueryString["iProductCategoryID"] != "" && Request.QueryString["iProductCategoryID"] != null)
            {
                //## Set label to modify
                lblAddModify.Text = "Modify Product Category";

                clsProductCategoryRecord = new clsProductCategoryRecord(clsUser, Convert.ToInt32(Request.QueryString["iProductCategoryID"]));

                //### Populate the form
                popFormData();
            }
            else
            {
                //### Set label to add
                lblAddModify.Text = "Add Product Category";

                clsProductCategoryRecord = new clsProductCategoryRecord(clsUser);
            }
            Session["clsProductCategoryRecord"] = clsProductCategoryRecord;
        }
        else
        {
            clsProductCategoryRecord = (clsProductCategoryRecord)Session["clsProductCategoryRecord"];
        }
    }

    protected void btnBack_Click(object sender, ImageClickEventArgs e)
    {
        //### Go back to previous page
        Response.Redirect("frmProductCategoryView.aspx");
    }

    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        //### Validate registration process
        bool bCanSave = true;

        //bCanSave = clsValidation.IsNullOrEmpty(txtProductTitle, reqTitle, bCanSave);
        //bCanSave = clsValidation.IsNullOrEmpty(txtDubblePrice, reqDubblePrice, bCanSave);

        if (bCanSave == true)
        {
            SaveData();
        }
        else
        {
            reqContainerDiv.Visible = true;
        }
    }

    protected void btnClear_Click(object sender, ImageClickEventArgs e)
    {
        reqContainerDiv.Visible = false;

        //txtProductTitle.Text = "";
        //clsValidation.SetValid(txtProductTitle, reqTitle);

        //txtDubblePrice.Text = "";
        //clsValidation.SetValid(txtDubblePrice, reqDubblePrice);
    }

    #endregion

    #region POPULATE DATA METHODS

    private void popFormData()
    {
        txtCategoryName.Text = clsProductCategoryRecord.strProductCategoryName;
        txtCategoryType.Text = clsProductCategoryRecord.strProductCategoryType;
        txtCategoryDescription.Text = clsProductCategoryRecord.strCategoryDescription;
        //txtDubblePrice.Text = clsProductsRecord.dblPrice.ToString();

        //if (!string.IsNullOrEmpty(clsProductsRecord.strPathToImages))
        //{
        //    lblUniquePath.Text = clsProductsRecord.strPathToImages;
        //    getList(clsProductsRecord.strPathToImages);

        //    //### Set Current Master Image
        //    List<string> lstImageFileName = (List<string>)Session["lstImageFileNames"];

        //    foreach (string strImageFileName in lstImageFileName)
        //    {
        //        if (strImageFileName == clsProductsRecord.strMasterImage)
        //        {
        //            RadioButton rdbMasterImage = dlImages.Items[lstImageFileName.IndexOf(strImageFileName)].FindControl("rdbMainImage") as RadioButton;
        //            rdbMasterImage.Checked = true;
        //            break;
        //        }
        //    }
        
    }

    #endregion

    #region SAVE DATA METHODS

    private void SaveData()
    {

        //### Add / Update
        clsProductCategoryRecord.dtAdded = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
        clsProductCategoryRecord.iAddedBy = clsUser.iUserID;
        clsProductCategoryRecord.dtEdited = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
        clsProductCategoryRecord.iEditedBy = clsUser.iUserID;
        clsProductCategoryRecord.strProductCategoryName = txtCategoryName.Text;
        clsProductCategoryRecord.strProductCategoryType = txtCategoryType.Text;
        clsProductCategoryRecord.strCategoryDescription = txtCategoryDescription.Text;
        clsProductCategoryRecord.bIsActive = true;

        clsProductCategoryRecord.Update();

        //### redirect back to view page
        Response.Redirect("frmProductCategoryView.aspx");
    }

    #endregion

}